<?php
namespace app\farm\model;

use app\BaseModel;
use think\facade\Db;

class BagAtv extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_bag_atv';




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        if(isset($data['new_coupon'])){

            $new_coupon = $data['new_coupon'];

            unset($data['new_coupon']);
        }

        if(isset($data['share_coupon'])){

            $share_coupon = $data['share_coupon'];

            unset($data['share_coupon']);
        }

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        if(isset($new_coupon)&&isset($share_coupon)){

            $this->updateSome($id,$data['uniacid'],$new_coupon,$share_coupon);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-01 14:41
     * @功能说明:添加优惠券
     */
    public function updateSome($id,$uniacid,$new,$share){

        $coupon_model = new BagAtvCoupon();

        $dis = [

            'atv_id' => $id
        ];

        $coupon_model->where($dis)->delete();

        if(!empty($new)){

            foreach ($new as $key=>$value){

                $new[$key]['uniacid'] = $uniacid;

                $new[$key]['atv_id']  = $id;

                $new[$key]['type']    = 1;

            }

            $coupon_model->saveAll($new);
        }

        if(!empty($share)){

            foreach ($share as $k=>$v){

                $share[$k]['uniacid'] = $uniacid;

                $share[$k]['atv_id']  = $id;

                $share[$k]['type']    = 2;

            }

            $coupon_model->saveAll($share);
        }

        return true;
    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['new_coupon'])){

            $new_coupon = $data['new_coupon'];

            unset($data['new_coupon']);
        }

        if(isset($data['share_coupon'])){

            $share_coupon = $data['share_coupon'];

            unset($data['share_coupon']);
        }

        $res = $this->where($dis)->update($data);

        if(isset($new_coupon)&&isset($share_coupon)){

            $this->updateSome($dis['id'],$data['uniacid'],$new_coupon,$share_coupon);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-01 18:15
     * @功能说明:检查
     */
    public function changeData($data){

        if(isset($data['imgs'])){

            $data['img'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';
        }

        $id = !empty($input['id'])?$input['id']:'';

        $where[] = ['status','>',-1];

        $where[] = ['uniacid','=',$data['uniacid']];

        if(!empty($id)){

            $where[] = ['id','<>',$id];

        }

        $list = $this->where($where)->select()->toArray();

        if(!empty($list)){

            foreach ($list as $value){

                $res = is_time_cross($value['start_time'],$value['end_time'],$data['start_time'],$data['end_time']);

                if($res==false){

                    return ['code'=>500,'msg'=>'该时间段已有活动'];
                }

            }
        }

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-03-21 15:04
     * @功能说明:正在进行的活动
     */
    public function activeIng($uniacid,$id = ''){

        $dis[] = ['start_time','<',time()];

        $dis[] = ['end_time','>',time()];

        $dis[] = ['status','=',1];

        $dis[] = ['uniacid','=',$uniacid];

        if(!empty($id)){

            $dis[] = ['id','<>',$id];
        }

        $data = $this->where($dis)->find();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-19 16:08
     * @功能说明:开启默认
     */
    public function updateOne($id){

        $user_id = $this->where(['id'=>$id])->value('user_id');

        $res = $this->where(['user_id'=>$user_id])->where('id','<>',$id)->update(['status'=>0]);

        return $res;
    }






}